<template>
  <div class="app-container">
    <!-- 搜索区域优化 -->
    <el-card class="search-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
        <el-form-item label="用户名" prop="sysUser.nickName">
          <el-input
            v-model="queryParams.sysUser.nickName"
            placeholder="请输入用户名"
            clearable
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="题目内容" prop="dtQuestion.title">
          <el-input
            v-model="queryParams.dtQuestion.title"
            placeholder="请输入题目内容"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="答题时间" prop="answerTime">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            style="width: 360px"
          />
        </el-form-item>
        <el-form-item label="是否答对" prop="isCorrect">
          <el-select v-model="queryParams.isCorrect" placeholder="请选择" clearable style="width: 120px">
            <el-option label="是" :value="0" />
            <el-option label="否" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮区域 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">答题记录列表</span>
        <div class="action-buttons">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['question:record:export']"
          >导出</el-button>
          <el-button
            :type="showDeleted ? 'warning' : 'info'"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="toggleRecycleBin"
          >{{ showDeleted ? '返回列表' : '回收站' }}</el-button>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>
      </div>

      <!-- 表格区域 -->
      <el-table
        v-loading="loading"
        :data="recordList"
        @selection-change="handleSelectionChange"
        border
        stripe
        highlight-current-row
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="记录ID" align="center" prop="id" width="80" />
        <el-table-column label="用户名" align="center" prop="sysUser.nickName" width="120" />
        <el-table-column label="题目内容" align="left" prop="dtQuestion.title" :show-overflow-tooltip="true" min-width="300">
          <template slot-scope="scope">
            <div class="question-content" v-html="scope.row.dtQuestion.title"></div>
          </template>
        </el-table-column>
        <el-table-column label="答题时间" align="center" prop="answerTime" width="160">
          <template slot-scope="scope">
            {{ parseTime(scope.row.answerTime) }}
          </template>
        </el-table-column>
        <el-table-column label="是否答对" align="center" prop="isCorrect" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isCorrect === 0 ? 'success' : 'danger'" size="mini">
              {{ scope.row.isCorrect === 0 ? '正确' : '错误' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="isDeleted" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.isDeleted"/>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { listRecord, exportRecord } from "@/api/question/record";

export default {
  name: "Record",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 答题记录表格数据
      recordList: [],
      // 日期范围
      dateRange: [],
      // 选中数组
      ids: [],
      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sysUser: {nickName: null},
        dtQuestion: {title: null},
        beginTime: null,
        endTime: null,
        isCorrect: null,
        isDeleted: '0'
      },
      showDeleted: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询答题记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // 处理时间范围
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      } else {
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.isDeleted = this.showDeleted ? '1' : '0';
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = { ...this.queryParams };
      if (this.dateRange && this.dateRange.length === 2) {
        queryParams.beginTime = this.dateRange[0];
        queryParams.endTime = this.dateRange[1];
      }
      this.download('question/record/export', queryParams, `record_${new Date().getTime()}.xlsx`);
    },
    toggleRecycleBin() {
      this.showDeleted = !this.showDeleted;
      this.queryParams.isDeleted = this.showDeleted ? '1' : '0';
      this.handleQuery();
    }
  }
};
</script>

<style scoped>
.search-card {
  margin-bottom: 15px;
}

.box-card {
  margin-bottom: 15px;
}

.card-title {
  font-size: 16px;
  font-weight: bold;
}

.action-buttons {
  float: right;
}

.question-content {
  padding: 8px 0;
  line-height: 1.5;
  text-align: left;
}

.question-content :deep(p) {
  margin: 0;
}

.el-card :deep(.el-card__header) {
  padding: 10px 15px;
  border-bottom: 1px solid #ebeef5;
}

.el-table {
  margin: 10px 0;
}

.el-form-item {
  margin-bottom: 15px;
}

.clearfix:after {
  content: '';
  display: table;
  clear: both;
}
</style>
